package yxy.game.pm2.active.command.redeemcode;

import org.joda.time.DateTime;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_兑换码;
import yxy.game.pm2.active.module.subordinateclass.GetRedeemCode;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActRedeemCodeDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

@MessageType(proto = Command.ActRedeemCodeDrawReward, classtype = ActiveMessage.ActRedeemCodeDrawRewardRequest.class)
public class CMD_Active_兑换码_DrawReward extends Command<ActRedeemCodeDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActRedeemCodeDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActRedeemCodeDrawRewardResponse.Builder response = ActiveMessage.ActRedeemCodeDrawRewardResponse.newBuilder();

		String redeemcode = request.getRedeemcode();

		ActiveModule_兑换码 am = ActiveModule_兑换码.get(redeemcode);
		if (am == null) {
			send(messageSource, response, Result.ActNoRedeemCode);
			return;
		}

		DateTime now = DateTime.now();
		if (now.isAfter(am.getLosetime())) {
			am.setState(am.invalid);
			am.set();
		}

		if (am.getState() != am.effective) {
			send(messageSource, response, Result.ActInvalid);
			return;
		}

		if (am.getPcodes().contains(pcode)) {
			send(messageSource, response, Result.ActInvalid);
			return;
		}

		ArrayList<Currency> curs = am.getCurs();

		if (am.getRedeemtype() == am.opoc) {
			String batch = am.getBatch();
			GetRedeemCode grc = GetRedeemCode.get(pcode);
			if (grc == null) {
				send(messageSource, response, Result.DataError);
				return;
			}

			ArrayList<String> opocbatchs = grc.getOpocbatchs();
			if (opocbatchs.contains(batch)) {
				send(messageSource, response, Result.ActHadGet);
				return;
			}

			am.getPcodes().add(pcode);
			am.setState(am.getover);
			am.set();

			grc.getOpocbatchs().add(batch);
			grc.set();
		}

		if (am.getRedeemtype() == am.opnc) {
			am.getPcodes().add(pcode);
			am.setState(am.getover);
			am.set();
		}

		if (am.getRedeemtype() == am.npoc) {
			am.getPcodes().add(pcode);
			am.set();
		}

		new CurrencyHandle().addCurrency(pcode, CCReason.活动_兑换码, curs);

		for (int i = 0; i < curs.size(); i++) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();

			Currency c = curs.get(i);
			item.setType(c.getType().getNumber());
			item.setDataId(c.getDataId());
			item.setNumber(c.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActRedeemCodeDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
